/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package org.finesys.system.controller;

import java.util.Arrays;
import java.util.List;

import org.finesys.common.constants.CacheConstants;
import org.finesys.common.core.constants.RestApiConfig;
import org.finesys.common.core.module.R;
import org.finesys.common.excel.annotaion.ResponseExcel;
import org.finesys.common.log.core.annotation.LogAnnotation;
import org.finesys.common.log.core.enums.OperateType;
import org.finesys.common.security.core.annotation.Inner;
import org.finesys.system.api.entity.SysPublicParam;
import org.finesys.system.constants.ControllerRestApiConfig;
import org.finesys.system.service.SysPublicParamService;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;

/**
 * 公共参数
 */
@RestController
@AllArgsConstructor
@RequestMapping(ControllerRestApiConfig.PARAM)
@Tag(description = "param", name = "公共参数配置")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysPublicParamController {

    private final SysPublicParamService sysPublicParamService;

    /**
     * 通过key查询公共参数值
     *
     * @param publicKey
     * @return
     */
    @Inner(value = false)
    @Operation(description = "查询公共参数值", summary = "根据key查询公共参数值")
    @GetMapping("/publicValue/{publicKey}")
    public R<?> publicKey(@PathVariable("publicKey") String publicKey) {
        return R.success(sysPublicParamService.getSysPublicParamKeyToValue(publicKey));
    }


    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping(RestApiConfig.PAGE)
    public R<?> getPage(Page<SysPublicParam> page, SysPublicParam SysPublicParam) {
        return R.success(sysPublicParamService.findPage(page, SysPublicParam));
    }

    @GetMapping(RestApiConfig.DETAILS)
    public R<?> getDetail(@ParameterObject SysPublicParam param) {
        return R.success(sysPublicParamService.getOne(Wrappers.query(param), false));
    }

    /**
     * 通过id查询角色信息表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping(RestApiConfig.GET + "/{id}")
    public R<?> findById(@PathVariable("id") Long id) {
        return R.success(sysPublicParamService.getById(id));
    }

    /**
     * 新增角色信息表
     *
     * @param sysRole 角色信息表
     * @return R
     */
    @Operation(summary = "新增公共参数", description = "新增公共参数")
    @LogAnnotation(title = "新增公共参数", operateType = OperateType.INSERT)
    @PostMapping(RestApiConfig.ADD)
    @PreAuthorize("@pms.hasPermission('sys_params_add')")
    public R<?> save(@RequestBody SysPublicParam sysPublicParam) {
        return R.success(sysPublicParamService.save(sysPublicParam));
    }


    /**
     * 修改参数
     *
     * @param sysPublicParam 参数
     * @return R
     */
    @Operation(summary = "修改参数", description = "修改参数")
    @LogAnnotation(title = "修改参数", operateType = OperateType.UPDATE)
    @PutMapping(value = RestApiConfig.UPDATE)
    @PreAuthorize("@pms.hasPermission('sys_params_edit')")
    @CacheEvict(value = CacheConstants.PARAMS_DETAILS, key = "#sysPublicParam.publicId")
    public R<?> updateById(@RequestBody SysPublicParam sysPublicParam) {
        return R.success(sysPublicParamService.updateById(sysPublicParam));
    }


    /**
     * 通过id删除公共参数
     *
     * @param ids ids
     * @return R
     */
    @Operation(description = "删除公共参数", summary = "删除公共参数")
    @LogAnnotation(title = "删除公共参数", operateType = OperateType.DELETE)
    @DeleteMapping(value = RestApiConfig.DELETE)
    @PreAuthorize("@pms.hasPermission('sys_params_delete')")
    @CacheEvict(value = CacheConstants.PARAMS_DETAILS, allEntries = true)
    public R<?> removeById(@RequestBody Long[] ids) {
        return R.success(sysPublicParamService.removeBatchByIds(Arrays.asList(ids)));
    }

    /**
     * 导出excel 表格
     *
     * @return
     */
    @ResponseExcel
    @GetMapping(value = RestApiConfig.EXPORT_EXCEL)
    @PreAuthorize("@pms.hasPermission('sys_params_export')")
    public List<SysPublicParam> export() {
        return sysPublicParamService.list();
    }

    /**
     * 同步参数
     *
     * @return R
     */
    @LogAnnotation(title = "同步参数", operateType = OperateType.OTHER)
    @PutMapping(value = RestApiConfig.SYNC)
    @PreAuthorize("@pms.hasPermission('sys_params_sync')")
    public R<?> sync() {
        return sysPublicParamService.syncParamCache();
    }

}
